package cn.wolfcode.qo;

import org.springframework.util.StringUtils;

/**
 * 抽取的查询条件
 *
 * @author 杨兴健
 * @Date 2020/7/8 8:10
 */
public class QueryObject {
    public String getKeyword() {
        return keyword;
    }

    public QueryObject setKeyword(String keyword) {
        if (!StringUtils.isEmpty(keyword)) {
            this.keyword = keyword;
        }
        return this;
    }

    /**
     * 关键字
     */
    private String keyword;
    /**
     * 当前页码
     */
    private Integer currentPage = 1;
    /**
     * 每页显示条数
     */
    private Integer pageSize = 5;

    public Integer getCurrentPage() {
        return currentPage;
    }

    public QueryObject setCurrentPage(Integer currentPage) {
        this.currentPage = currentPage;
        return this;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public QueryObject setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
        return this;
    }

    /**
     * 数据库查询的页数
     *
     * @return
     */
    public int getStart() {
        return (currentPage - 1) * pageSize < 0 ? 0 : (currentPage - 1) * pageSize;
    }

    /**
     * 检查查询条件
     *
     * @param count
     */
    public void checkPage(int count) {
        int maxPage = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
        if (currentPage > maxPage) {
            this.setCurrentPage(maxPage);
        }
    }
}
